﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.废气仪
{
    public class fqymka504 : fqybass
    {
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            bool breturn = base.openCOM(sSet1);
            commex.ReadTimeout = 500;
            //commex.ReceivedBytesThreshold = 52;//达到50字节以后才开始触发串口
            commex.DataReceived += comGAS_DataReceived;
            return breturn;
        }

        /// <summary>
        /// 发送指令到仪器
        /// </summary>
        /// <param name="cmd">通信命令
        /// 1:开始测量命令;
        /// 2:停止测量命令;
        /// 3:实时数据获取命令;
        /// 4:反吹;
        /// 5:调零命令;
        /// 6:实时状态获取命令;
        /// 7:背景空气测定命令;
        /// 8:环境空气测定命令;
        /// 9:HC残留测量;
        /// 10:泄漏检查;
        /// 11:设置调零气体命令;(预留)
        /// 12:尾气校准命令;(预留)
        /// 13:锁定键盘操作命令;
        /// 14:解锁键盘操作命令;
        /// 15:HC燃料设置命令;(预留)
        /// 16:设置点火数命令;(预留)
        /// 17:设置冲程数命令;(预留)
        /// 18:背景空气数据获取;
        /// 19:环境空气数据获取;
        /// 20:HC残留数据获取;
        ///21:管路压力标定命令;(预留)
        ///22:油温标定命令;(预留)
        ///23:自动调零设置命令;(预留)
        ///24:停止当前动作检查命令;(预留)
        ///25:抽气动作检查命令;(预留)
        ///26:抽环境空气动作检查命令;(预留)
        ///27:通零气动作检查命令;(预留)
        ///28:通校准气动作检查;(预留)
        /// 29:通检查气动作检查命令;(预留)</param>
        /// <returns></returns>
        public override bool SendDeviceCmd(int cmd)
        {
            base.SendDeviceCmd(cmd);
            switch (cmd)
            {
                case 1:
                    {
                        sendcmd(0x79, 0x82);
                        objzsy.SendDeviceCmd(); break;
                    }         // 1:开始测量命令;
                case 2: { sendcmd(0x7A, 0x81); break; }         // 2:停止测量命令;
                case 3: { sendcmd(0x60, 0x9B); break; }     // 3:实时数据获取命令;
                case 4: { sendcmd(0x65, 0x96); break; }         // 4:反吹;
                case 5: { sendcmd(0x67, 0x94); break; }         // 5:调零命令;
                case 6: { sendcmd(0x61, 0x9A); break; }     // 6:实时状态获取命令;
                case 7: { sendcmd(0x62, 0x99); break; }     // 7:背景空气测定命令;
                case 8: { sendcmd(0x63, 0x98); break; }     // 8:环境空气测定命令;
                case 9: { sendcmd(0x64, 0x97); break; }         // 9:HC残留测量;
                case 10: { sendcmd(0x66, 0x95); break; }        // 10:泄漏检查;
                //case 11: { sendcmd(0x68, 0x); break; }        // 11:设置调零气体命令;
                //case 12: { sendcmd(0x69, 0x); break; }        // 12:尾气校准命令;
                case 13: { sendcmd(0x6A, 0x91); break; }        // 13:锁定键盘操作命令;
                case 14: { sendcmd(0x6B, 0x90); break; }        // 14:解锁键盘操作命令;
                //case 15: { sendcmd(0x6C, 0x); break; }        // 15:HC燃料设置命令;
                //case 16: { sendcmd(0x6D, 0x); break; }        // 16:设置点火数命令;
                //case 17: { sendcmd(0x6E, 0x); break; }        // 17:设置冲程数命令;
                case 18: { sendcmd(0x6F, 0x8C); break; }        // 18:背景空气数据获取;
                case 19: { sendcmd(0x70, 0x8B); break; }        // 19:环境空气数据获取;
                case 20: { sendcmd(0x71, 0x8A); break; }        // 10:HC残留数据获取;
                //case 21: { sendcmd(0x, 0x); break; }        // 21:管路压力标定命令;
                //case 22: { sendcmd(0x, 0x); break; }        // 22:油温标定命令;
                //case 23: { sendcmd(0x, 0x); break; }        // 23:自动调零设置命令;
                case 24: { sendcmd(0x78, 0x83); break; }        // 24:停止当前动作检查命令;
                case 25: { sendcmd(0x7B, 0x80); break; }        // 25:抽气动作检查命令;
                case 26: { sendcmd(0x7C, 0x7F); break; }        // 26:抽环境空气动作检查命令;
                //case 27: { sendcmd(0x, 0x); break; }        // 27:通零气动作检查命令;
                //case 28: { sendcmd(0x, 0x); break; }        // 28:通校准气动作检查;
                case 29: { sendcmd(0x7F, 0x7C); break; }        // 29:通检查气动作检查命令;</param>
            }

            return true;
        }


        private bool sendcmd(byte cCmd2, byte cCmd4)
        {
            byte[] cCmdBuf = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
            commex.ReceivedBytesThreshold = 24;
            cCmdBuf[0] = 0x02;
            cCmdBuf[1] = cCmd2;
            cCmdBuf[2] = 0x03;
            cCmdBuf[3] = cCmd4;//
            string sneirong = string.Format(@"{0:X000} {1:X000} {2:X000} {3:X000}", cCmdBuf[0], cCmdBuf[1], cCmdBuf[2], cCmdBuf[3]);
            //sendGAScom(sneirong);
            if (commex.IsOpen)
            {
                Byte[] m_byte = new Byte[4];
                for (int i = 0; i < 4; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                commex.Write(m_byte, 0, 4);
            }
            return true;
        }
        private List<byte> buffer = new List<byte>(4096);
        void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            try
            {
                if (commex.IsOpen)
                {
                    System.Threading.Thread.Sleep(100);
                    int n = commex.BytesToRead;
                    byte[] buf = new byte[n];
                    commex.Read(buf, 0, n);

                    buffer.AddRange(buf);

                    for (int i = 0; i < buffer.Count - 3; i++)
                    {

                        if (buffer[0 + i] == 0x06 && buffer[1 + i] == 0x60 && buffer[2 + i] == 0x1B)
                        {
                            if (buffer.Count - i > 27)
                            {
                                byte uSum1 = 0;
                                for (int kk = 0; kk < 27; kk++)
                                {
                                    uSum1 += (byte)(buffer[kk + i]);
                                }
                                uSum1 = (byte)(~uSum1 + 1);
                                short uSum = 0;
                                uSum = (short)(buffer[i + 27]);
                                if (uSum == uSum1)
                                {

                                    float.TryParse(string.Format("{0}", (buffer[23 + i] * 256 + buffer[24 + i]) * 0.001), out m_fLamda);

                                    float.TryParse(string.Format("{0}", (buffer[15 + i] * 256 + buffer[16 + i]) * 0.1), out m_fyw);
                                    float.TryParse(string.Format("{0}", (buffer[11 + i] * 256 + buffer[12 + i]) * 0.01), out m_fO2);
                                    m_iNo = buffer[9 + i] * 256 + buffer[10 + i];
                                    m_iHc = buffer[7 + i] * 256 + buffer[8 + i];

                                    float.TryParse(string.Format("{0}", (buffer[5 + i] * 256 + buffer[6 + i]) * 0.01), out m_fCo);
                                    float.TryParse(string.Format("{0}", (buffer[3 + i] * 256 + buffer[4 + i]) * 0.01), out m_fCo2);
                                    jcyq();
                                    buffer.Clear();
                                    i = 0;
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (buffer[0 + i] == 0x06 && buffer[1 + i] == 0x61 && buffer[2 + i] == 0x0A)
                        {
                            if (buffer[3 + i] == 0x80)
                            {
                                m_fDLLYJ = true;
                            }
                            if (buffer[3 + i] == 0x10)
                            {
                                m_fxlbs = true;
                            }
                            else
                            {
                                m_fxlbs = false;
                            }
                            m_fxl = buffer[5 + i];
                            buffer.Clear();
                            break;
                        }
                        //else //帧头不正确时，记得清除
                        //{
                        //    buffer.RemoveAt(0);
                        //}
                    }
                }
            }
            catch
            {

            }


            //-------------------------------

        }

    }
}
